//
//  LYCustomerIDAlertView.swift
//  LYOnlineBanking
//
//  Created by 李永彬 on 2025/7/30.
//

import UIKit

class LYCustomerIDAlertView: UIView {
    
    var confirmActionClosure: (() -> Void)?
    
    var birthdayButtonActionClosure: ((String) -> Void)?
    
    private lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.boldSystemFont(ofSize: 16)
        label.textColor = UIColor(hex: "#000000")
        label.textAlignment = .center
        label.text = LYlocalizedString("ID card information")
        return label
    }()

    private let nameLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 14, weight: .regular)
        label.textColor = UIColor(hex: "#000000")
        label.textAlignment = .left
        label.text = LYlocalizedString("Real name")
        return label
    }()
    
    let nameTextField: LYTextField = {
        let textField = LYTextField()
        textField.layer.masksToBounds = true
        textField.layer.borderWidth = 1.0
        textField.layer.borderColor = UIColor(hex: "#D9D9D9").cgColor
        textField.layer.cornerRadius = 8.0
        return textField
    }()
    
    private let idLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 14, weight: .regular)
        label.textColor = UIColor(hex: "#000000")
        label.textAlignment = .left
        label.text = LYlocalizedString("ID number")
        return label
    }()
    
    lazy var idTextField: LYTextField = {
        let textField = LYTextField()
        textField.layer.masksToBounds = true
        textField.layer.borderWidth = 1.0
        textField.layer.borderColor = UIColor(hex: "#D9D9D9").cgColor
        textField.layer.cornerRadius = 8.0
        textField.placeholder = LYlocalizedString("ID number")
        return textField
    }()
    
    let birthdayLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 14, weight: .regular)
        label.textColor = UIColor(hex: "#000000")
        label.textAlignment = .left
        label.text = LYlocalizedString("Birthday")
        return label
    }()
    
    let birthdayClickableView: LYClickableView = {
        let clickableView = LYClickableView()
        clickableView.text = "23/03/2024"
        clickableView.placeholder = LYlocalizedString("Birthday")
        clickableView.textColor = .darkGray
        clickableView.font = .systemFont(ofSize: 16, weight: .regular)
        clickableView.arrowTintColor = .gray
        clickableView.layer.masksToBounds = true
        clickableView.layer.borderWidth = 1.0
        clickableView.layer.borderColor = UIColor(hex: "#D9D9D9").cgColor
        clickableView.layer.cornerRadius = 8.0
        return clickableView
    }()
    
    lazy var confirmButton: UIButton = {
        let button = UIButton(type: .custom)
        button.addTarget(self, action: #selector(confirmButtonAction(_:)), for: .touchUpInside)
        button.setTitleColor(UIColor(hex: "#FFFFFF"), for: .normal)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 16, weight: .semibold)
        button.setTitle(LYlocalizedString("Confirm"), for: .normal)
        button.backgroundColor = UIColor(hex: "#1E4FF3")
        button.layer.masksToBounds = true
        button.layer.cornerRadius = (45 * xScale) / 2.0
        return button
    }()
    
    private let confirmInformationLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 14, weight: .regular)
        label.textColor = UIColor(hex: "#333333")
        label.textAlignment = .left
        label.text = LYlocalizedString("confrm information")
        return label
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        addSubview(titleLabel)
        addSubview(nameLabel)
        addSubview(nameTextField)
        addSubview(idLabel)
        addSubview(idTextField)
        addSubview(birthdayLabel)
        birthdayClickableView.onClick = { [weak self] in
            guard let self = self else { return }
            self.birthdayButtonActionClosure?(birthdayClickableView.text ?? "")
        }
        addSubview(birthdayClickableView)
        addSubview(confirmInformationLabel)
        addSubview(confirmButton)
        commonInitSubviews()
    }
    
    func commonInitSubviews() {
        
        let leftR = 25.0
        titleLabel.snp.makeConstraints { make in
            make.leading.trailing.equalToSuperview().inset(16)
            make.top.equalToSuperview().offset(30)
            make.height.equalTo(20)
        }
        
        nameLabel.snp.makeConstraints { make in
            make.leading.trailing.equalToSuperview().inset(leftR)
            make.top.equalTo(titleLabel.snp.bottom).offset(25)
            make.height.equalTo(20)
        }
        
        nameTextField.snp.makeConstraints { make in
            make.leading.trailing.equalToSuperview().inset(leftR)
            make.top.equalTo(nameLabel.snp.bottom).offset(10)
            make.height.equalTo(50)
        }
        
        idLabel.snp.makeConstraints { make in
            make.leading.trailing.equalTo(nameLabel)
            make.top.equalTo(nameTextField.snp.bottom).offset(15)
            make.height.equalTo(20)
        }
        
        idTextField.snp.makeConstraints { make in
            make.leading.trailing.equalTo(nameTextField)
            make.top.equalTo(idLabel.snp.bottom).offset(10)
            make.height.equalTo(50)
        }
        
        birthdayLabel.snp.makeConstraints { make in
            make.leading.trailing.equalTo(nameLabel)
            make.top.equalTo(idTextField.snp.bottom).offset(15)
            make.height.equalTo(20)
        }
        
        birthdayClickableView.snp.makeConstraints { make in
            make.leading.trailing.equalTo(nameLabel)
            make.top.equalTo(birthdayLabel.snp.bottom).offset(10)
            make.height.equalTo(50)
        }
        
        confirmInformationLabel.snp.makeConstraints { make in
            make.leading.trailing.equalTo(birthdayLabel)
            make.top.equalTo(birthdayClickableView.snp.bottom).offset(15)
            make.height.equalTo(20)
        }
        
        confirmButton.snp.makeConstraints { make in
            make.bottom.equalToSuperview().offset(-30)
            make.leading.trailing.equalTo(nameTextField)
            make.height.equalTo(45 * xScale)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setModel(uploadIDCardData: UploadIDCardData) {
        self.nameTextField.textField.text = uploadIDCardData.sarcling
        self.idTextField.textField.text = uploadIDCardData.typo
        if let birthdayText = uploadIDCardData.agohood,
           birthdayText.count > 0,
           birthdayText != "//" {
            self.birthdayClickableView.text = birthdayText
        } else {
            self.birthdayClickableView.text = nil
        }
    }
    
    @objc func confirmButtonAction(_ sender: UIButton) {
        confirmActionClosure?()
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesBegan(touches, with: event)
        self.endEditing(true)
    }
}

class LYClickableView: UIView {
    
    // MARK: - 属性
    
    /// 点击回调
    var onClick: (() -> Void)?
    
    /// 文本内容
    var text: String? {
        didSet {
            textField.text = text
        }
    }
    
    
    /// 文本内容
    var placeholder: String? {
        didSet {
            textField.placeholder = placeholder
        }
    }
    
    /// 文本颜色
    var textColor: UIColor = .black {
        didSet {
            textField.textColor = textColor
        }
    }
    
    /// 文本字体
    var font: UIFont = .systemFont(ofSize: 16) {
        didSet {
            textField.font = font
        }
    }
    
    /// 箭头图标
    var arrowImage: UIImage? = UIImage(named: "arrow_alert") {
        didSet {
            arrowImageView.image = arrowImage
        }
    }
    
    /// 箭头颜色
    var arrowTintColor: UIColor = .lightGray {
        didSet {
            arrowImageView.tintColor = arrowTintColor
        }
    }
    
    // MARK: - UI组件
    private lazy var textField: UITextField = {
        let textField = UITextField()
        textField.textColor = textColor
        textField.font = font
        textField.textAlignment = .left
        textField.isEnabled = false
        return textField
    }()
    
    private lazy var arrowImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.image = arrowImage
        imageView.tintColor = arrowTintColor
        imageView.contentMode = .scaleAspectFit
        return imageView
    }()
    
    private lazy var tapGesture: UITapGestureRecognizer = {
        let tap = UITapGestureRecognizer(target: self, action: #selector(viewTapped))
        return tap
    }()
    
    // MARK: - 初始化
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        setupUI()
    }
    
    // MARK: - UI设置
    
    private func setupUI() {
        backgroundColor = .white
        
        // 添加子视图
        addSubview(textField)
        addSubview(arrowImageView)
        
        // 添加点击手势
        addGestureRecognizer(tapGesture)
        isUserInteractionEnabled = true
        
        // 使用SnapKit设置约束
        setupConstraints()
    }
    
    private func setupConstraints() {
        textField.snp.makeConstraints { make in
            make.leading.equalToSuperview().offset(16)
            make.centerY.equalToSuperview()
            make.trailing.lessThanOrEqualTo(arrowImageView.snp.leading).offset(-8)
        }
        
        arrowImageView.snp.makeConstraints { make in
            make.trailing.equalToSuperview().offset(-16)
            make.centerY.equalToSuperview()
            make.width.height.equalTo(20)
        }
        
        self.snp.makeConstraints { make in
            make.height.equalTo(50)
        }
    }
    
    // MARK: - 点击事件
    
    @objc private func viewTapped() {
        onClick?()
    }
}

